/**
 *
 */

package cn.antia.cwin.db;

import java.util.*;

import cn.antia.cep.common.*;
import cn.antia.cep.db.DBException;
import cn.antia.cep.busi.BusiException;
import cn.antia.cwin.Cele;

/**
 * @author bin
 * @version 0.12 Oct 1, 2006
 * @date Sep 28, 2006
 */
public class DbService extends cn.antia.cwin.db.CwinDAO {
	/**
     * 查询属性
     *
     * @param table 表名
     * @return 属性列表List<ElementComsat>
     * @throws cn.antia.cep.busi.BusiException
     *          BusiException
     */
    public List<Cele> getAttribute(String table) throws BusiException {
        //这里的SQL写法是MYSQL的写法
        AntiaLog.debug(new StringBuffer().append("查询了表 ").append(table).append(" 的结构。").toString());
        try {
            return findTableDesc(table);
        } catch (DBException dbe) {
            BusiException be = new BusiException("002",dbe.getMessage());
            SysExceptionTool.addExceptionInfo(be);
            throw be;
        }
    }

    /**
     * 更新
     *
     * @param table    表名
     * @param elements 要更新的元素
     * @param conditions  conditions elements
     * @return 影响行数
     * @throws cn.antia.cep.busi.BusiException
     *          BusiException
     */
    public int update(String table, List elements, List conditions) throws BusiException {
        throw new BusiException("000","方法未实现。");
    }

    /**
     * 主从表删除方法
     *
     * @param ptable     primary table
     * @param id         id
     * @param stable     sec table
     * @param foreignKey foreign key
     * @return int
     * @throws BusiException e
     */
    public int delete(String ptable, int id, String stable, String foreignKey) throws BusiException {
        try {
            int lines = delete(ptable, id);
            AntiaLog.info(new StringBuffer("删除了").append(ptable).append("表").append(lines).append("条主表数据。").toString());
            String sql = new StringBuffer("delete from ").append(stable).append(" where ").append(foreignKey).append(" = ").append(id).toString();
            int slines = update(sql);
            AntiaLog.info(new StringBuffer("删除了").append(slines).append("条从表数据。").toString());
            return lines;
        } catch (DBException dbe) {
            BusiException be = new BusiException("002",dbe.getMessage());
            SysExceptionTool.addExceptionInfo(be);
            throw be;
        }
    }
	
}
